﻿
using System;
using System.Threading;
namespace HBase {
    /// <summary>
    /// Cookie类
    /// </summary>
    public class CookieHelper {
        public static MyDict<String ,long> ticks = new MyDict<string ,long>();
        public static MyDict<String ,String> tonken = new MyDict<string ,string>();
        public static MyDict<String ,String> ipDict = new MyDict<string ,string>();
        public static int tokenTime = -1;

        public static bool isTockenTimeOut(String tocken) {
            if( tokenTime == -1 ) {
                return false;
            }
            if( !ticks.ContainsKey(tocken) ) {

                return true;
            }



            int time = ( int ) ((DateTime.Now.Ticks - ticks[tocken]) / HUtil.ticksSecRate);
            return time > tokenTime;

        }

        public static void addtoken(String value) {
            String id = Thread.CurrentThread.ManagedThreadId.ToString();
            ticks.addOverride(value ,DateTime.Now.Ticks);
            tonken.addOverride(id ,value);

        }
        static String getToken() {
            String id = Thread.CurrentThread.ManagedThreadId.ToString();
            String token = null;
            if( tonken.ContainsKey(id) ) {
                token = tonken[id];
            }
            return token;
        }

        public static void addIp(String value) {
            String id = Thread.CurrentThread.ManagedThreadId.ToString();
    
            ipDict.addOverride(id ,value);

        }
        public static String getIp() {
            String id = Thread.CurrentThread.ManagedThreadId.ToString();
            String token = null;
            if( ipDict.ContainsKey(id) ) {
                token = ipDict[id];
            }
            return token;
        }

        


        /// <summary>
        /// 添加一个Cookie
        /// </summary>
        /// <param name="cookiename">cookie名</param>
        /// <param name="cookievalue">cookie值</param>
        /// <param name="expires">过期时间 DateTime</param>

        public static string LoginName() {

            String token = getToken();


            if( String.IsNullOrWhiteSpace(token) ) {
                return "";
            }
            byte[] c = Convert.FromBase64String(token);
            byte[] r = new byte[c.Length - 15];
            int index = 0;
            for( int i = 0;i < c.Length;i++ ) {
                if( i < 30 ) {
                    if( i % 2 == 1 ) {
                        if( index == r.Length ) {
                            break;
                        }
                        r[index++] = c[i];
                    }
                } else {
                    if( index == r.Length ) {
                        break;
                    }
                    r[index++] = c[i];
                }
            }
            String name = System.Text.Encoding.UTF8.GetString(r);
            return name;
        }
    }
}
